from mysql.connector import Error
from mysql.connector import pooling

# 定义连接池
dbconfig = {
    "host": "106.55.231.210",
    "user": "idea",
    "password": "FAxwAKYpnGJm58TZ",
    "database": "idea"
}

# 创建连接池
try:
    pool = pooling.MySQLConnectionPool(
        pool_name="mypool",
        pool_size=5,
        **dbconfig
    )
except Error as e:
    print(f"<<<连接池创建失败，错误为 {e}")


def connect_fetch():
    """从连接池获取连接，查询MySQL数据库"""
    try:
        # 从连接池中获取连接
        connection = pool.get_connection()

        if connection.is_connected():
            cursor = connection.cursor()
            # 查询语句
            query = "SELECT model_id FROM bqb_writing GROUP BY model_id"
            cursor.execute(query)

            # 获取查询结果
            records = cursor.fetchall()
            return records
    except Error as e:
        return []
        print(f"<<<数据库操作失败，错误为 {e}")
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()


if __name__ == "__main__":
    connect_fetch()
